package com.bing;

import com.bing.entity.AnnotEvent;
import com.bing.entity.Event;
import com.bing.entity.inheritance.*;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.util.Date;
import java.util.List;

/**
 * Created by kaibb on 15-10-29.
 */
public class HelloHibernate {
    private SessionFactory sessionFactory;

    @Before
    public void before() {
        // A SessionFactory is set up once for an application!
        final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
                .configure() // configures settings from hibernate.cfg.xml
                .build();
        try {
            sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
        } catch (Exception e) {
            // The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory
            // so destroy it manually.
            System.out.println(e);
            StandardServiceRegistryBuilder.destroy(registry);
        }
    }

    @Test
    public void hello() {
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.save(new Event("bing", new Date()));
        session.save(new Event("follow ing Event", new Date()));
        session.getTransaction().commit();
        session.close();

        session = sessionFactory.openSession();
        session.beginTransaction();
        Query query = session.createQuery("from Event ");
        for (Event event : ((List<Event>) query.list())) {
            System.out.println(event);
        }
        session.close();
    }

    /**
     * 测试注解方式的实体映射
     */
    @Test
    public void helloAnnot() {
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.save(new AnnotEvent("bing", new Date()));
        session.save(new AnnotEvent("follow ing Event", new Date()));
        session.getTransaction().commit();
        session.close();

        session = sessionFactory.openSession();
        session.beginTransaction();
        Query query = session.createQuery("from AnnotEvent ");
        for (AnnotEvent event : ((List<AnnotEvent>) query.list())) {
            System.out.println(event);
        }
        session.close();
    }


    @Test
    public void testSingleTable() {
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        SingleTable singleTable = new SingleTable();
        singleTable.setName("singleTable");
        session.save(singleTable);

        SingleTableA singleTableA = new SingleTableA();
        singleTableA.setName("singleTableA");
        singleTableA.setApro("APro");
        session.save(singleTableA);

        SingleTableB singleTableB = new SingleTableB();
        singleTableB.setName("SingleTableB");
        singleTableB.setbPro("BPro");
        session.save(singleTableB);

        session.getTransaction().commit();
        session.close();
    }

    @Test
    public void testPerClass() {
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        PerClass singleTable = new PerClass();
        singleTable.setName("perClass");
        session.save(singleTable);

        PerClassA perClassA = new PerClassA();
        perClassA.setName("perClassA");
        perClassA.setaPro("APro");
        session.save(perClassA);

        PerClassB perClassB = new PerClassB();
        perClassB.setName("SingleTableB");
        perClassB.setbPro("BPro");
        session.save(perClassB);

        session.getTransaction().commit();
        session.close();
    }

    @Test
    public void testJoin() {
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        JoinD joinD = new JoinD();
        joinD.setName("joinD");
        session.save(joinD);

        JoinDA joinA = new JoinDA();
        joinA.setName("joinA");
        joinA.setaPro("APro");
        session.save(joinA);

        JoinDB joinB = new JoinDB();
        joinB.setName("joinB");
        joinB.setbPro("BPro");
        session.save(joinB);

        JoinDBC joinBC = new JoinDBC();
        joinBC.setName("joinBC");
        joinBC.setBcPro("BCPro");
        session.save(joinBC);

        session.getTransaction().commit();
        session.close();
    }
}
